package com.sname.car.record.controller;
import com.sname.car.common.constants.Constants;
import com.sname.car.common.exception.CarException;
import com.sname.car.common.resp.ApiResult;
import com.sname.car.common.rest.BaseController;
import com.sname.car.record.entity.Record;
import com.sname.car.record.entity.RideContrail;
import com.sname.car.record.service.BikeGeoService;
import com.sname.car.record.service.RecordService;
import com.sname.car.user.entity.UserElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * Created By  醉美柳舞之众星捧月
 * @author songx
 * @date 2018/9/26 13:59
 */
@RestController
@RequestMapping("/record")
public class RecordController extends BaseController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private RecordService recordService;
    @Autowired
    private BikeGeoService bikeGeoService;
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/26 17:01
     * Description  查询骑行历史
     */
    @RequestMapping("/list/{id}")
    public ApiResult<List<Record>> listRecord(@PathVariable("id") Long lastId) {
        ApiResult<List<Record>> resp = new ApiResult<>();
        try {
            UserElement userElement = getCurrentUserInformation();
            List<Record> list = recordService.listRecord(userElement.getUserId(), lastId);
            resp.setData(list);
        } catch (Exception e) {
            log.error("Fail to find record list of this user ", e);
            resp.setMessage(e.getMessage());
            resp.setSuccess(false);
            resp.setCode(Constants.HTTP_ERROR_CODE);
        }
        return resp;
    }
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/26 17:02
     * Description  查询骑行的轨迹
     */
    @RequestMapping("/contrail/{recordNo}")
    public ApiResult rideContrail(@PathVariable("recordNo") String recordNo) {
        ApiResult<Object> resp = new ApiResult<>();
        try {
            RideContrail contrail = bikeGeoService.rideContrail("ride_contrail", recordNo);
            if (StringUtils.isEmpty(contrail)) {
                throw new CarException("Fail to Query ride record");
            }
            resp.setData(contrail);
        } catch (Exception e) {
            resp.setCode(Constants.HTTP_ERROR_CODE);
            resp.setMessage(e.getMessage());
            resp.setSuccess(false);
        }
        return resp;
    }
}
